package com.itheima.edu.info.manger.controller;
import com.itheima.edu.info.manger.domain.Teacher;
import com.itheima.edu.info.manger.service.TeacherService;

import java.util.Scanner;


public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    public void start() {
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
//                System.out.println("添加");
                    addTeacher();
                    break;
                case 2:
                  //  System.out.println("删除");
                    deleteTeacherById();
                    break;
                case 3:
                   // System.out.println("修改");
                    updateTeacher();
                    break;
                case 4:
//                    System.out.println("查看");
                    getAllTeacher();
                    break;
                case 5:
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("你的输入有误，请重新输入！");
                    break;
            }
        }
    }
//修改老师信息
    private void updateTeacher() {
        String uid = inputTeachId();
        Teacher newTeacher = inputTeachInfo(uid);
        TeacherService.updateTeacher(newTeacher,uid);
        System.out.println("修改成功");
    }
//键盘录入id
    private String inputTeachId() {
        String id;
        while (true) {
            System.out.println("输入老师id：");
            id = sc.next();
            boolean exits = TeacherService.getExits(id);
            if (exits){
                break;
            }else{
                System.out.println("你输入的id不存在，请重新输入！");
            }
        }
        return id;
    }

    //删除
    public void deleteTeacherById() {
        String did=inputTeachId();
        TeacherService.deleteTeacherById(did);
        System.out.println("删除成功！");
    }
//查询
    public  void getAllTeacher(){
       Teacher[] teachers= TeacherService.getAllTeacher();
       if (teachers==null){
           System.out.println("查无数据，添加后查询");
           return;
       }
       System.out.println("学号\t\t姓名\t年龄\t生日");
       for (int i = 0; i < teachers.length; i++) {
           Teacher teacher = teachers[i];
           if (teacher!=null){
           System.out.println(teacher.getId()+"\t"+teacher.getName()+"\t"+teacher.getAge()+"\t\t"+teacher.getBirthday());
       }
       }
   }
//添加老师信息
    public void addTeacher() {
        String id;
        while (true) {
            System.out.println("输入要添加的id");
            id = sc.next();
            boolean flag = TeacherService.getExits(id);
            if (flag) {
                System.out.println("该学号已被占用，请重新输入");
            } else {
               break;
            }
        }
        Teacher teach = inputTeachInfo(id);
        boolean result = TeacherService.addTeacher(teach);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    private Teacher inputTeachInfo(String id) {
        System.out.println("输入要添加的姓名：");
        String name = sc.next();
        System.out.println("输入要添加的年龄：");
        String age = sc.next();
        System.out.println("输入要添加的生日：");
        String birthday = sc.next();
        Teacher teach = new Teacher();
        teach.setId(id);
        teach.setName(name);
        teach.setAge(age);
        teach.setBirthday(birthday);
        return teach;
    }

}
